@extends('admin.layout.base')
@section('styles')
@endsection
@section('content')
    <div class="panel-content" id="app" v-cloak>
        <div class="panel-body">
            <div class="panel-header" style="height: 40px;border-bottom: 1px solid #E4E7ED">
                <div class="panel-title">等级管理</div>
                
            </div>
            <div class="panel-main">
                <div class="panel-btns">
                    <el-button type="success" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                    <el-button type="primary" icon="fa fa-plus" @click="create()" size="small"> 新增</el-button>
                   
                    <el-button type="info" icon="fa fa-trash" @click="multiDelete()" size="small"> 删除</el-button>
                </div>
                
                <el-table v-loading="loading"
                          element-loading-text="努力加载中..."
                          element-loading-spinner="el-icon-loading"
                          ref="multipleHandle"
                          tooltip-effect="dark"
                          @selection-change="handleSelectionChange"
                          :data="listData" border>
                    <el-table-column align="center" type="selection" width="30"></el-table-column>
                    <el-table-column align="center" prop="id" label="编号" width="60"></el-table-column>
                    <el-table-column align="center" prop="icon" label="等级图标" width="80">
                        <template slot-scope="scope"> 
                        	<img :src="scope.row.icon" style="height: 25px;width: 25px;"/> 
                        </template>
                    </el-table-column>
                   <el-table-column align="center" prop="name" label="等级名称" width="120"></el-table-column>
                   
                   
                    <el-table-column align="center" prop="limit_number" label="需要完成任务数" width="180"></el-table-column>
                   
                    
                    <el-table-column align="center" prop="level_nums" label="会员数" width="90">
                        <template slot-scope="scope"> <span v-html="scope.row.level_nums"></span> </template>
                    </el-table-column>
                    
                     
                  
                    <el-table-column align="center" label="操作" width="200">
                        <template slot-scope="scope">
                          
                            <el-button @click="edit(scope.row)" icon="fa fa-edit" type="primary" size="mini" plain> 编辑</el-button>
                            <el-button @click="destroy(scope.row.id)" icon="fa fa-trash" type="info" size="mini" plain> 删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                
            </div>
        </div>
        <!-- 表单弹窗-->
        <el-dialog
                v-if="dialog.show"
                :title="dialog.title"
                :top="dialog.top"
                :visible.sync="dialog.show"
                :width="dialog.width">           
            <el-form :model="form" ref="form" label-width="100px">
                <el-form-item :rules="[{ required: true, message: '请输入等级名称', trigger: 'blur'}]" label="等级名称：" prop="name">
                    <el-input clearable v-model="form.name" placeholder="请输入等级名称"></el-input>
                </el-form-item>
                <el-form-item label="图标：" prop="icon">
                    <el-upload class="category-uploader" name="image"
                               :action="upload_image"
                               :show-file-list="false"
                               :limit="1"
                               :on-success="uploadSuccess"
                               :before-upload="uploadBefore">
                        <img v-if="form.icon" :src="form.icon" class="category-img">
                        <i v-else class="el-icon-plus category-uploader-icon"></i>
                        <div slot="tip" class="el-upload__tip">图片大小不能超过 2MB!</div>
                    </el-upload>
                </el-form-item>
                
                <el-form-item :rules="[{ required: true, message: '请输入需要完成任务数', trigger: 'blur'}]" label="需要完成任务数：" prop="limit_number">
                    <el-input clearable v-model="form.limit_number" placeholder="完成任务数"></el-input>
                </el-form-item>
               
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialog.show = false">取 消</el-button>
                <el-button type="primary" @click="update()">确 定</el-button>
              </span>
        </el-dialog>
    </div>
@endsection
@section('scripts')
    <script>
        let app = new Vue({
            el: '#app',
            data: function() {
                return {
                    listData: [],//列表数据
                    upload_image: UPLOAD_IMAGE,
                    loading: false,
                    dialog: {
                        title: '',
                        top: '15vh',
                        show: false,
                        width: '600px'
                    },
                    multipleSelection:[],//多选项
                    form:null,
                }
            },
            created(){
                this.list();
            },
            methods: {
                //分页
                changePage(val) {
                    this.params.page = val;
                    this.list();
                },
                uploadSuccess(res) {
                    this.form.icon = res.data.path;
                },

                uploadBefore(file) {
                    const isLt2M = file.size / 1024 / 1024 < 2;
                    if (!isLt2M) {
                        this.$message.error('图片大小不能超过 2MB!');
                    }
                    return isLt2M;
                },
                //列表
                list(){
                    let that = this;
                    this.loading = true;
                    setTimeout(()=>{
                        $.get("{{ route('admin.userLevel.list') }}",this.params,function (res) {
                            if(res.code){
                                that.$message.error(res.msg);
                            }else{
                              //  alert(res.data);
                                that.listData = res.data;
                                that.loading = false;
                            }
                        },'JSON');
                    },500);
                },
                //新增
                create(){
                    this.form = {
                        id: 0,
                        icon:'',
                        name: '',
                        limit_number:'',
                        _token: "{{csrf_token()}}"
                    };
                    this.dialog.title = '新增等级';
                    this.dialog.show = true;
                },
                //编辑
                edit(info){
                	this.form = {
                            id: info.id,
                            name: info.name,
                           
                            icon: info.icon,
                           
                            limit_number: info.limit_number,
                            _token: "{{csrf_token()}}"
                        };
                        this.dialog.title = '编辑等级';
                        this.dialog.show = true;
                },
                update(){
                    this.$refs['form'].validate((valid) => {
                        if (valid) {
                            let that = this;
                            $.post("{{ route('admin.userLevel.update') }}",this.form,function (res) {
                                if(res.code){
                                    that.$message.error(res.msg);
                                }else{
                                    that.$message({
                                        type: 'success',
                                        duration: '1000',
                                        message: res.msg,
                                        onClose:() =>{
                                            that.dialog.show = false;
                                            that.list();
                                        }
                                    });
                                }
                            },'JSON');
                        }
                    });
                },
                
                // 多选删除
                multiDelete(){
                    if(this.multipleSelection.length == 0){
                        this.$message.error('请至少选择一项你要删除的项');return false;
                    }
                    this.destroy(this.multipleSelection);
                },
                handleSelectionChange(val) {
                    let select = [];
                    if(val.length > 0){
                        val.filter((item)=>{
                            if(select.indexOf(item.id) === -1){
                                select.push(item.id)
                            }
                        });
                        this.multipleSelection = select;
                    }else{
                        this.multipleSelection = [];
                    }
                },
                //删除
                destroy(id){
                    let that = this;
                    this.$confirm('确定要删除选择的等级信息吗？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        $.post("{{ route('admin.userLevel.destroy') }}",{id: id, _token: "{{csrf_token()}}"},function (res) {
                            if(res.code){
                                that.$message.error(res.msg);
                            }else{
                                that.list();
                            }
                        },'JSON');
                    }).catch(() => {});
                },
            }
        });
    </script>
@endsection
